﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web;

using System.Runtime.Serialization.Formatters;



namespace Model
{
    public class ShopCar
    {
        /// <summary>
        /// 总计
        /// </summary>
        public static decimal Total;
        public static Dictionary<string, OrderItem> cartList = cartList = new Dictionary<string, OrderItem>();
        /// <summary>
        /// 获取Session中的购物车列表
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, OrderItem> GetCar()
        {

            if (HttpContext.Current.Session["ByCart"] != null)
            {
                cartList = HttpContext.Current.Session["ByCart"] as Dictionary<string, OrderItem>;
            }
            else
            {                
                HttpContext.Current.Session["ByCart"] = cartList;
            }
            return cartList;
        }
        /// <summary>
        /// 获取总金额
        /// </summary>
        /// <returns></returns>
        public static decimal GetTotal()
        {

            cartList = GetCar();
            decimal total = 0;
            foreach (OrderItem item in cartList.Values)
            {
                item.PayMoney = item.Num * Convert.ToInt32(item.liber.liberMoney);
                total += item.PayMoney;
            }
            Total = total;
            return total;
        }
        ///// <summary>
        ///// 根据商品编号获取购物车信息
        ///// </summary>
        ///// <param name="pid">商品编号</param>
        ///// <returns></returns>
        //public static OrderItem GetItem(int pid)
        //{
        //    OrderItem oi = new OrderItem();
        //    if (GetItem（pid）!= null)
        //    {

        //    }
        //    else
        //    {

        //    }
        //    return oi;
        //}
        ///// <summary>
        ///// 减少商品数量
        ///// </summary>
        ///// <param name="pid">商品编号</param>
        ///// <param name="num">商品数量</param>
        ///// <param name="msg">结果</param>
        ///// <returns></returns>
        //public static bool Reduce(int pid,int num,out string msg)
        //{
        //    msg = "";
        //    return true;
        //}

        /// <summary>
        /// 添加商品到购物车
        /// </summary>
        /// <param name="pInfo">商品</param>
        /// <param name="pNum">商品购买数量</param>
        /// <param name="op">true（添加） false（减少）</param>
        public static void AddCart(liber pInfo,int pNum, bool op)
        {
            OrderItem oi = null;
            if (op)//添加
            {
                if (cartList.ContainsKey(pInfo.liberID.ToString()))
                {
                    cartList[pInfo.liberID.ToString()].Num++;
                }
                else
                {
                    oi = new OrderItem() { Num = pNum, liber = pInfo, OrderID = -1 };
                    cartList.Add(pInfo.liberID.ToString(), oi);
                    HttpContext.Current.Session["ByCart"] = cartList;
                }
            }
            else//减少
            {
                if (cartList.ContainsKey(pInfo.liberID.ToString()))
                {
                    if (cartList[pInfo.liberID.ToString()].Num > 1)
                    {
                        cartList[pInfo.liberID.ToString()].Num--;
                    }
                }
            }
        }
        /// <summary>
        /// 移除商品
        /// </summary>
        /// <param name="pid">商品编号</param>
        /// <param name="msg">结果</param>
        /// <returns></returns>
        public static bool Remove(int pid, out string msg)
        {
            bool result = false;
            msg = "失败";
            foreach (OrderItem item in cartList.Values)
            {
                if (cartList.ContainsKey(pid.ToString()))
                {
                    cartList.Remove(pid.ToString());
                    result = true;
                    msg = "成功";
                }                
            }            
            return result;
        }
        /// <summary>
        /// 清空商品
        /// </summary>
        public static void Clear()
        {
            HttpContext.Current.Session["ByCart"] = null;
            cartList.Clear();
        }
    }
}
